﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RazakiBLL;
using RazakiBLL.Entities;
using RazakiBLL.Managers;

namespace RazakiSite.PagesNews
{
    public partial class NewEdit : BasePage
    {
        UowRazaki _uowRazaki = new UowRazaki();

        private short ChildId
        {
            get { return UtilitiesUI.GetQueryString<short>("childid"); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.SetDefaultFormControls(null, DrpDLTag.ClientID);

            if (IsPostBack)
                return;

            ConfigControls();
        }

        protected override void ConfigControls()
        {
            DrpDLTag.QuickBind(_uowRazaki.NewTagRepository.GetChildernInSameRootByChildId(ChildId));
            DrpDLTag.InsertDefaultItem();

            DrpDLTag.SelectedValue = ChildId.ToString();

            NewsActions.NewTagFirstParentId = _uowRazaki.NewTagRepository.GetFirstParentByLastChildId(ChildId).Id;
        }

        private void ClearControls()
        {
            TxtBTitle.Text =
                UCEditor.UCContent =
                TxtBPublisher.Text = string.Empty;
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            short newTagId = short.Parse(DrpDLTag.SelectedValue);
            string errorMessage;
            string title = TxtBTitle.Text;
            string text = UCEditor.UCContent;
            string source = TxtBPublisher.Text;
            New newEntity;

            UCInfo.Hide = false;

            if (!ValidateControls(out errorMessage))
            {
                UCInfo.InfoType = EInfoType.error;
                UCInfo.Text = errorMessage;
                return;
            }

            newEntity = new New()
                                {
                                    Title = title,
                                    Text = text,
                                    ViewCount = 0,
                                    UserId = UtilitiesUI.GetCurrentUserId(),
                                    Publisher = source,
                                    TagId = newTagId,
                                    UpdateDate = DateTime.Now
                                };

            _uowRazaki.NewRepository.Add(newEntity);
            if (_uowRazaki.Save() > 0)
            {
                UCInfo.InfoType = EInfoType.success;
                UCInfo.Text = UtilitiesUI.Sentences(ESentences.success);

                ClearControls();
            }
            else
            {
                UCInfo.InfoType = EInfoType.error;
                UCInfo.Text = UtilitiesUI.Sentences(ESentences.error);
            }
        }

        private bool ValidateControls(out string errorMessage)
        {
            short newTagId = short.Parse(DrpDLTag.SelectedValue);
            string title = TxtBTitle.Text;
            string text = UCEditor.UCContent;
            string source = TxtBPublisher.Text;

            errorMessage = string.Empty;

            if (newTagId == -1)
            {
                errorMessage = string.Format(UtilitiesUI.Sentences(ESentences.requiredSelect), "گروه");
                return false;
            }

            if (string.IsNullOrWhiteSpace(title))
            {
                errorMessage = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "عنوان");
                return false;
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                errorMessage = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "متن");
                return false;
            }

            if (string.IsNullOrWhiteSpace(source))
            {
                errorMessage = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "منبع");
                return false;
            }

            return true;
        }
    }
}